package cn.com.jinhao.provider.business.controller;

import cn.com.jinhao.hotline.commons.controller.BaseController;
import cn.com.jinhao.hotline.commons.exception.ParamErrorException;
import cn.com.jinhao.hotline.commons.util.RestResponse;
import cn.com.jinhao.hotline.commons.vo.business.feedback.FeedbackCondition;
import cn.com.jinhao.hotline.commons.vo.business.feedback.FeedbackRequestVO;
import cn.com.jinhao.provider.business.entity.Feedback;
import cn.com.jinhao.provider.business.service.AttachService;
import cn.com.jinhao.provider.business.service.FeedbackService;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * <p>Description: 反馈服务</p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2020/05/30
 * @since 2020-05-30
 */

@RestController
@RequestMapping("/feedback")
@Api(tags = {"反馈服务"})
public class FeedbackController extends BaseController {
    @Resource
    FeedbackService feedbackService;
    @Resource
    AttachService attachService;

    @ApiOperation(value = "新增反馈")
    @PostMapping
    public RestResponse<String> add(@ApiParam(name = "feedbackRequestVO", value = "反馈请求信息", required = true) @Validated(FeedbackRequestVO.Create.class) @RequestBody FeedbackRequestVO feedbackRequestVO, BindingResult result) throws Exception {
        validate(result);
        feedbackService.add(feedbackRequestVO);
        return new RestResponse<>(null);
    }

    @ApiOperation(value = "更新反馈")
    @PutMapping
    public RestResponse<String> update(@ApiParam(name = "feedbackRequestVO", value = "反馈请求信息", required = true) @Validated(FeedbackRequestVO.Update.class) @RequestBody FeedbackRequestVO feedbackRequestVO, BindingResult result) throws Exception {
        validate(result);
        feedbackService.update(feedbackRequestVO);
        return new RestResponse<>(null);
    }

    @ApiOperation(value = "反馈审核")
    @PutMapping(value = "/audit")
    public RestResponse<String> audit(@ApiParam(name = "feedbackRequestVO", value = "反馈请求信息", required = true) @Validated(FeedbackRequestVO.Audit.class) @RequestBody FeedbackRequestVO feedbackRequestVO, BindingResult result) throws Exception {
        validate(result);
        feedbackService.update(feedbackRequestVO);
        return new RestResponse<>(null);
    }

    @ApiOperation(value = "删除反馈")
    @DeleteMapping
    public RestResponse<String> delete(@ApiParam(name = "id", value = "主键ID") @RequestParam String id) throws Exception {
        feedbackService.delete(id);
        return new RestResponse<>(null);
    }

    @ApiOperation(value = "反馈详情")
    @GetMapping(value = "/detail")
    public RestResponse<Feedback> detail(@ApiParam(name = "feedbackCode", value = "反馈编码") @RequestParam String feedbackCode) throws Exception {
        Feedback feedback = feedbackService.selectByCode(feedbackCode);
        if (Optional.ofNullable(feedback).isPresent()) {
            feedback.setAttachList(attachService.selectByHostCode(feedbackCode));
        }
        return new RestResponse<>(feedback);
    }

    @ApiOperation(value = "反馈查询", notes = "通过条件查询反馈信息")
    @GetMapping(value = "/selectByConditions")
    public RestResponse<java.util.Map<String, Object>> selectByConditions(@ApiParam(name = "", value = "") @Validated @RequestBody FeedbackCondition conditions, BindingResult result) throws Exception {
        validate(result);
        return new RestResponse<>(feedbackService.selectByCondition(conditions));
    }
}
